#!/bin/sh
## 
## Copyright (c) Brian de Alwis. All rights reserved.
## 
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions
## are met:
## 
##  1. Redistributions of source code must retain the above copyright
##     notice, this list of conditions and the following disclaimer.
##  2. Redistributions in binary form must reproduce the above copyright
##     notice, this list of conditions and the following disclaimer in the
##     documentation and/or other materials provided with the distribution.
##  3. All advertising materials mentioning features or use of this software
##     must display the following acknowledgement:
##  	This product includes software developed by Brian de Alwis.
##  4. The name of Brian de Alwis may not be used to endorse or promote
##     products derived from this software without his specific prior
##     written permission.
## 
## THIS SOFTWARE IS PROVIDED BY BRIAN DE ALWIS ``AS IS'' AND ANY
## EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
## PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL BRIAN DE ALWIS BE LIABLE
## FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
## OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
## BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
## WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
## OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
## 

tmpfile=${TMPDIR:-/tmp}/fjar.$$

usage()
{
    echo "use: $0 [-i] regexp [files.jar|directories]..." 1>&2
    echo 1>&2
    echo "Find jars with matching class names.  If no jars specified," 1>&2
    echo "then use jars on CLASSPATH.  If directories specified, then" 1>&2
    echo "uses all jar files found under that directory strucure." 1>&2
    echo "  -i   ignore case" 1>&2
    exit 1
}

notempty() {
    if [ $# -ne 1 ]; then
	echo "error: notempty has wrong number of arguments";
	exit 1
    fi
    test "`ls -s "$1" | awk '{ print $1 }'`" != 0
}

grepargs=""

while getopts i c; do
    case $c in
    i)  grepargs="-i";;
    \?)	usage;;
    esac
done
shift `expr $OPTIND - 1`
if [ $# -lt 1 ]; then usage; fi

pattern="$1"
trap "rm -f $tmpfile; exit 0" 0 1 2 3 15

if [ $# -eq 1 ]; then
    OIFS=$IFS; IFS=:
    set -- $CLASSPATH
    IFS=$OIFS
fi

# $1 = pattern, $2 = jarfile
doSearch() {
    if [ $# -ne 2 ]; then echo "error: doSearch(): wrong arguments"; fi
    if [ -f "$2" ]; then
	jar tf "$2" | grep $grepargs "$1" >$tmpfile
	if notempty $tmpfile; then
	    echo "==> $2"
	    cat $tmpfile
	fi
	rm -f $tmpfile
    fi
}

for jarfile in "$@"; do
    if [ -f "$jarfile" ]; then
	doSearch "$pattern" "$jarfile"
    elif [ -d "$jarfile" ]; then
	find $jarfile -name '*.jar' -type f -print | \
	    while read subjarfile; do 
		doSearch "$pattern" "$subjarfile"
	    done
    fi
done
